<?php

/**
 * api 接口控制器
 *
 * @author pufan
 */

namespace Huodong\Controller;

use Huodong\Controller\ApiBaseController;
use Library\Prizes\Prizes;
use Library\SMS;
use Library\User;
use Library\CardCode;
use Library\PrizesOrder;
use Think\Log;

class ApiController extends ApiBaseController{
    function _initialize() {
        if (!IS_POST) { //禁止get访问
            exit;
        }
        parent::_initialize();
    }

    /*
     *  微信摇一摇调用的API接口， errcode 错误码 403 服务器拒绝请求 504 在摇一摇内停留时间过长
     *
     *  数据状态码:0 中奖 2 为中奖 3 摇奖次数已用完
     *  data为数据字段 money为金额单位为分 ad_desc 为广告描述 ad_pic 为广告图片链接
     *  ad_link 广告链接
     */
    public function shake() {
        $dayShakeCount = S('huodong_day_shake_count'. $_SESSION['open_id']);
        if ($dayShakeCount == false) {
            S('huodong_day_shake_count'. $_SESSION['open_id'], 1 , 3600*24);
        } else {
            $dayShakeCount = redisIncr('huodong_day_shake_count'. $_SESSION['open_id']);
            Log::record($dayShakeCount.'-'.C('HUODONG_DAY_SHAKE_COUNT'));
            if ($dayShakeCount > C('HUODONG_DAY_SHAKE_COUNT')) {
                $this->api_return(403, '24小时内的抽奖次数,超过了系统限制,请稍后再来。');
            }
        }
        try {
            $shake = new \Library\HuodongShake(2);
            $prizes = $shake->get_prizes();
        } catch (\Exception $e) {
            \Think\Log::record('shake catch Exception'.$e->getMessage());
        }

        if ($prizes) {
            $prizes_type = $prizes->get_type();
            switch ($prizes_type) {
                case Prizes::HONGBAO_RAND:
                    \Think\Log::record('prizes_type: is HONGBAO_RAND');
                    $this->api_return(0,'ok', 0, 'prizes', $prizes->get_view_desc());
                    break;
                case Prizes::AD:
                    \Think\Log::record('prizes_type: is AD');
                    $this->api_return(0,'ok', 2, 'prizes', $prizes->get_view_desc());
                    break;
                case Prizes::LINK:
                    \Think\Log::record('prizes_type: is LINK');
                    $this->api_return(0,'ok', 4, 'prizes', $prizes->get_view_desc());
                    break;
                case Prizes::Entity:
                    \Think\Log::record('prizes_type: is Entity');
                    $this->api_return(0,'ok', 5, 'prizes', $prizes->get_view_desc());
                    break;
                default:
                    \Think\Log::record('prizes_type: is Unknown');
                    break;
            }
        }
        $this->api_return(0, 'ok', 1);
    }

    public function tixian() {
        $user_wallet = new \Library\UserWallet();

        $this->api_return(0,'ok', $user_wallet->withdraw());
    }

    /*
     * 发送手机验证码
     */
    function getPhoneCaptcha() {
        $sms = new SMS();
        $phone = I('post.phone_number', '', '/1[34578]{1}\d{9}$/');
        if (empty($phone)) {
            $this->api_return(0, 'ok', 1);
        }
        $nick = $_SESSION['wxuser']['nickname'] ? $_SESSION['wxuser']['nickname'] : '游客';
        $shark_openid = $_SESSION['open_id'];
        if (empty($nick) || empty($shark_openid)) {
            \Think\Log::record('getPhoneCaptcha nickname or open_id empty');
           $this->api_return(0, 'ok', 2);
        }

        $dx_count = S($_SESSION['open_id'].':dx_count');
        if ($dx_count >= C('PHONEVERIFY_COUNT_LIMIT')) {
            $this->api_return(0, 'ok', 4);  //每天限制发送3条短信
        }

        $rand_code = sprintf("%06d", mt_rand(0, 999999));

        $data['name'] = $nick;
        $data['code'] = $rand_code;
        $result = $sms->sendMsg('SMS_9690344', $phone, $data);

        if ($result != 0) {
             $this->api_return(0, 'ok', 3);
        }

        $sms_list = S('phone_verify:'.$shark_openid.':'.$phone);
        if ($sms_list) {
            $sms_list .= ','.$rand_code;
        } else {
            $sms_list = $rand_code;
        }

        S($_SESSION['open_id'].':dx_count', $dx_count+1, strtotime(date('Y-m-d',strtotime('+1 day'))) - time());

        S('phone_verify:'.$shark_openid.':'.$phone, $sms_list ,C('PHONEVERIFY_TIMEOUT'));
        $this->api_return(0, 'ok', 0);
    }

    /*
     * 验证码验证接口
     */
    /*function verifyPhoneCode() {
        $sms = new SMS();
        $code = I('post.captcha_number', '');
        $phone = I('post.phone_number', '', '/1[34578]{1}\d{9}$/');
        if (!$code) {
            $this->api_return(0, 'ok', 1);
        }

        $shark_openid = $_SESSION['open_id'];
        $sms_list = S('phone_verify:'.$shark_openid.':'.$phone);

        if ($sms_list) {
            $code_list = explode(',', $sms_list);
        } else {
            $this->api_return(0, 'ok', 2); //不匹配
        }

        foreach ($code_list as $key => $value) {
            if ($value == $code) {
                $user = User::getInstance();
                if ($user->addPhone($phone)) {
                    $this->api_return(0, 'ok', 0); //验证通过
                } else {
                    $this->api_return(0, 'ok', 3); //验证失败
                }
            }
        }

        $this->api_return(0, 'ok', 2); //不匹配
    }*/

    /*
     * 领取奖品
     */
    public function getPrizes() {
        $card_code = I('post.cid', '');

        \Think\Log::record($card_code);
        if (!$card_code) {
            $this->api_return(0, 'ok', 1);
        }

        $user = User::getInstance();
        if ($user->checkUserInfo() == FALSE) {
            $this->api_return(0, 'ok', 2);
        }

        $cardCode = CardCode::getInstance();
        if ($cardCode->getPrizes($card_code) == FALSE) {
            $this->api_return(0, 'ok', 3);
        }
        $this->api_return(0, 'ok', 0);
    }
    public function saveUserInfo() {
        $name = I('post.real_name');
        $gender = I('post.gender');
        $address = I('post.address');
		$idcode =  I('post.idcode');
        $code = I('post.captcha_number', '');
        $phone = I('post.phone_number', '', '/1[34578]{1}\d{9}$/');
        $card_code = $_POST['card_code'];

        \Think\Log::record($name.'-'.$address.'-'.$code.'-'.$phone);
        if (!$name || !$code || !$phone || !$card_code) {
            $this->api_return(0, 'ok', 1);
        }
        $shark_openid = $_SESSION['open_id'];
        $sms_list = S('phone_verify:'.$shark_openid.':'.$phone);

        if ($sms_list) {
            $code_list = explode(',', $sms_list);
        } else {
            $this->api_return(0, 'ok', 2); //不匹配
        }
        foreach ($code_list as $key => $value) {
            if ($value == $code) {
                $user = User::getInstance();
                if (!$user->addPhone($phone)) {
                    $this->api_return(0, 'ok', 3); //验证失败
                }
            }
        }

        $data['realname'] = $name;
        $data['gender'] = $gender;
        $data['address'] = $address;
		$data['idcode'] = $idcode;

        $user = User::getInstance();
        $user->saveCurrentUserInfo($data);

        if ($card_code) {
            $cardCode = CardCode::getInstance();
            $cardCode->getPrizes($card_code);
        }
        $this->api_return(0, 'ok', 0);
    }

}

